﻿using FarPoint.Web.Spread;
using System;
using System.Drawing;
using System.Web.UI.WebControls;

namespace ControlExplorer.samples.Footer
{
  public partial class Overview : SpreadDemoPage
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      if (IsPostBack) return;
      //Layout
      this.FpSpread1.Height = 300;
      this.FpSpread1.UseClipboard = false;
      this.FpSpread1.Sheets[0].GroupBarText = "拖拽列头到此进行分组。";

      //Spread
      this.FpSpread1.ActiveSheetView.GroupBarVisible = true;
      this.FpSpread1.ActiveSheetView.AllowGroup = true;

      this.FpSpread1.ActiveSheetView.GroupFooterVisible = true;

      this.FpSpread1.ActiveSheetView.ColumnCount = 13;
      this.FpSpread1.ActiveSheetView.DefaultColumnWidth = 30;
      this.FpSpread1.ActiveSheetView.Columns[5].Width = 90;
      this.FpSpread1.ActiveSheetView.Columns[6].Width = 120;
      this.FpSpread1.ActiveSheetView.Columns[7].Width = 90;
      this.FpSpread1.ActiveSheetView.Columns[9].Width = 180;

      this.FpSpread1.ActiveSheetView.RowCount = 7;

      if (this.FpSpread1.EnableClientScript)
      {
        this.FpSpread1.ActiveSheetView.Columns[11].Width = 100;
        this.FpSpread1.ActiveSheetView.Rows[6].Height = 1000;
      }

      this.FpSpread1.ActiveSheetView.ColumnFooter.Visible = true;
      this.FpSpread1.ActiveSheetView.ColumnFooter.RowCount = 2;
      this.FpSpread1.ActiveSheetView.ColumnFooter.DefaultStyle.ForeColor = Color.Purple;
      this.FpSpread1.ActiveSheetView.ColumnFooter.DefaultStyle.Border.BorderStyle = BorderStyle.Double;
      this.FpSpread1.ActiveSheetView.ColumnFooter.Columns[12].HorizontalAlign = HorizontalAlign.Left;


      this.FpSpread1.ActiveSheetView.ColumnFooter.Cells[0, 12].RowSpan = 2;

      //Frozen
      this.FpSpread1.ActiveSheetView.FrozenColumnCount = 2;
      this.FpSpread1.ActiveSheetView.FrozenRowCount = 2;

      //Value
      for (int r = 0; r < this.FpSpread1.ActiveSheetView.RowCount; r++)
      {
        for (int j = 0; j < this.FpSpread1.ActiveSheetView.ColumnCount; j++)
        {
          this.FpSpread1.ActiveSheetView.DataModel.SetValue(r, j, j + r * this.FpSpread1.ActiveSheetView.ColumnCount);
        }
      }

      //AggregationType of Column Footer
      int i = 0;
      this.FpSpread1.ActiveSheetView.Columns[i].AggregationType = FarPoint.Web.Spread.Model.AggregationType.Avg;
      this.FpSpread1.ActiveSheetView.ColumnFooter.Cells[0, i].Value = "平均";//Help Info of Column Footer
      i++;
      this.FpSpread1.ActiveSheetView.Columns[i].AggregationType = FarPoint.Web.Spread.Model.AggregationType.Count;
      this.FpSpread1.ActiveSheetView.ColumnFooter.Cells[0, i].Value = "数量";
      i++;
      this.FpSpread1.ActiveSheetView.Columns[i].AggregationType = FarPoint.Web.Spread.Model.AggregationType.CountA;
      this.FpSpread1.ActiveSheetView.ColumnFooter.Cells[0, i].Value = "非空数";
      i++;
      this.FpSpread1.ActiveSheetView.Columns[i].AggregationType = FarPoint.Web.Spread.Model.AggregationType.Max;
      this.FpSpread1.ActiveSheetView.ColumnFooter.Cells[0, i].Value = "最大值";
      i++;
      this.FpSpread1.ActiveSheetView.Columns[i].AggregationType = FarPoint.Web.Spread.Model.AggregationType.Min;
      this.FpSpread1.ActiveSheetView.ColumnFooter.Cells[0, i].Value = "最小值";
      i++;
      this.FpSpread1.ActiveSheetView.Columns[i].AggregationType = FarPoint.Web.Spread.Model.AggregationType.Product;
      this.FpSpread1.ActiveSheetView.ColumnFooter.Cells[0, i].Value = "乘积";
      i++;
      this.FpSpread1.ActiveSheetView.Columns[i].AggregationType = FarPoint.Web.Spread.Model.AggregationType.StDev;
      this.FpSpread1.ActiveSheetView.ColumnFooter.Cells[0, i].Value = "S.D.";
      this.FpSpread1.ActiveSheetView.ColumnFooter.Cells[1, i].CellType = new DoubleCellType();
      i++;
      this.FpSpread1.ActiveSheetView.Columns[i].AggregationType = FarPoint.Web.Spread.Model.AggregationType.StDevP;
      this.FpSpread1.ActiveSheetView.ColumnFooter.Cells[0, i].Value = "S.D.P";
      i++;
      this.FpSpread1.ActiveSheetView.Columns[i].AggregationType = FarPoint.Web.Spread.Model.AggregationType.Sum;
      this.FpSpread1.ActiveSheetView.ColumnFooter.Cells[0, i].Value = "合计";
      this.FpSpread1.ActiveSheetView.ColumnFooter.Cells[1, i].Value = "Sum:[{0}]";
      i++;
      this.FpSpread1.ActiveSheetView.Columns[i].AggregationType = FarPoint.Web.Spread.Model.AggregationType.Var;
      this.FpSpread1.ActiveSheetView.ColumnFooter.Cells[0, i].Value = "方差";
      this.FpSpread1.ActiveSheetView.ColumnFooter.Cells[1, i].CellType = new PercentCellType();
      i++;
      this.FpSpread1.ActiveSheetView.Columns[i].AggregationType = FarPoint.Web.Spread.Model.AggregationType.VarP;
      this.FpSpread1.ActiveSheetView.ColumnFooter.Cells[0, i].Value = "总体方差";
      i++;
      this.FpSpread1.ActiveSheetView.Columns[i].AggregationType = FarPoint.Web.Spread.Model.AggregationType.Custom;
      this.FpSpread1.ActiveSheetView.ColumnFooter.Cells[1, i].Value = "37.2~10.1";// "Custom";
      i++;
      this.FpSpread1.ActiveSheetView.Columns[i].AggregationType = FarPoint.Web.Spread.Model.AggregationType.None;
      this.FpSpread1.ActiveSheetView.ColumnFooter.Cells[0, i].Value = "None";
      this.FpSpread1.ActiveSheetView.ColumnFooter.Cells[1, 12].Value = "None";

      //Skin
      //DefaultSkins.Gradient1.Apply(this.FpSpread1);
    }

    protected void FpSpread1_Aggregate(object sender, FarPoint.Web.Spread.Model.AggregateArgs e)
    {
      if (e.Column == 11)
      {
        e.Result = "Custom";
      }
      if (e.Column == 12)
      {
        e.Result = "Aggregation:";
      }
    }

    protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
    {
      switch (e.Item.Value)
      {
        case "0":
          this.FpSpread1.EnableClientScript = true;
          this.FpSpread1.ActiveSheetView.Columns[11].Width = 100;
          this.FpSpread1.ActiveSheetView.Rows[6].Height = 1000;
          break;
        case "1":
          this.FpSpread1.EnableClientScript = false;
          this.FpSpread1.ActiveSheetView.Columns[12].Width = 30;
          this.FpSpread1.ActiveSheetView.Rows[6].Height = 22;
          break;
      }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
      this.FpSpread1.SavePdfToResponse("test.pdf");
    }
  }
}